﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace ModifyNamespace
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void BtnModify_Click(object sender, EventArgs e)
        {
            string strOldNS = txtOldNS.Text;//旧命名空间
            string strNewNS = txtNewNS.Text;//新命名空间
            string pl = txtProjectLocation.Text;
            //命名空间和路径不为空
            if (string.IsNullOrEmpty(strOldNS) ||string.IsNullOrEmpty(strNewNS)||string.IsNullOrEmpty(pl))
            {
                DialogResult dr = MessageBox.Show("请补全缺失信息");
                return;
            }

            if(string.IsNullOrEmpty( txtFilters.Text))
            {
                BatchModifyNS(pl, strOldNS, strNewNS);
            }
            else
            {
                BatchModifyNS(pl, strOldNS, strNewNS, txtFilters.Text);
            }
        }

        private void BtnLoadProject_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            //OpenFileDialog ofd = new OpenFileDialog();
            if(fbd.ShowDialog() ==DialogResult.OK)
            {
                txtProjectLocation.Text = fbd.SelectedPath;
            }
        }

        private void BatchModifyNS(string path,string oldNS,string newNS ,string filter = "*.*")
        {
            List<string> filters = filter.Split(new char[] { '|' }).ToList();
            List<string> files = GetAllFilesFromPathExt(path,filters);

            DialogResult dr = MessageBox.Show(string.Format("获取到文件{0}个，是否修改？",files.Count),"修改确认");
            if(dr == DialogResult.OK)
            {
                foreach (var item in files)
                {
                    try
                    {
                        ModifyFile(item, oldNS, newNS);
                    }
                    catch(Exception ex)
                    {

                    }
                }
                MessageBox.Show("修改完成！");
            }

        }

        private List<string> GetAllFilesFromPathExt(string path,List<string> filters)
        {
            List<string> lstFiles = new List<string>();
            //获取当前目录下的所有文件
            foreach (var item in filters)
            {
                string[] files = Directory.GetFiles(path, item);
                lstFiles.AddRange(files.ToList());
            }

            //获取当前路径下的所有目录
            List<string> directions = Directory.GetDirectories(path).ToList();
            foreach (var item in directions)
            {
                List<string> subFiles = GetAllFilesFromPathExt(item, filters);
                lstFiles.AddRange(subFiles);
            }

            return lstFiles;
        }
        private void ModifyFile(string filePath,string oldNS,string newNS)
        {
            string content = File.ReadAllText(filePath);
            content = Regex.Replace(content, oldNS, newNS);
            File.WriteAllText(filePath, content);
        }
    }
}
